package com.gitee.melin.bee.core.support;

import org.slf4j.helpers.MessageFormatter;

/** Created by admin on 17/3/3. */
public class Result<T> {

    /** 执行成功状态 */
    private boolean success = false;

    /** 执行描述信息 */
    private String message;

    /** 执行描述信息 */
    private T data;

    public Result() {}

    public Result(boolean success, String message, T data) {
        this.success = success;
        this.message = message;
        this.data = data;
    }

    public Result(boolean success, T data) {
        this.success = success;
        this.data = data;
    }

    public Result(boolean success) {
        this.success = success;
    }

    public static <R> Result<R> failureResult(String message, Object... params) {
        if (params.length > 0) {
            message = MessageFormatter.arrayFormat(message, params).getMessage();
        }
        return new Result<R>(false, message, null);
    }

    public static <R> Result<R> failureResult() {
        return new Result<R>(false, null, null);
    }

    public static <R> Result<R> successResult(String message, R data) {
        return new Result<R>(true, message, data);
    }

    public static <R> Result<R> successMessageResult(String message, Object... params) {
        if (params.length > 0) {
            message = MessageFormatter.arrayFormat(message, params).getMessage();
        }
        return new Result<R>(true, message, null);
    }

    public static <R> Result<R> successDataResult(R data) {
        return new Result<R>(true, null, data);
    }

    public static <R> Result<R> successResult() {
        return new Result<R>(true, null, null);
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public Result<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }
}
